using System;
using System.Windows;
using Microsoft.Practices.CompositeUI.Commands;

namespace UIAdapters
{
	public sealed class Shell : SCSFContrib.CompositeUI.WPF.ApplicationShellApplication<RootWorkItem, App>
	{
		[STAThread]
		public static void Main()
		{
			new Shell().Run();
		}

		protected override void AfterShellCreated()
		{
			base.AfterShellCreated();
			//register a command adapter that allows us to adapt ContentElements such as Hyperlinks
			ICommandAdapterMapService commandAdapterMapService = RootWorkItem.Services.Get<ICommandAdapterMapService>();
			commandAdapterMapService.Register(typeof(ContentElement), typeof(ContentElementCommandAdapter));
		}

		private class ContentElementCommandAdapter : EventCommandAdapter<ContentElement>
		{
			public ContentElementCommandAdapter()
			{
			}

			public ContentElementCommandAdapter(ContentElement invoker, string eventName)
				: base(invoker, eventName)
			{
			}

			protected override void OnCommandChanged(Command command)
			{
				base.OnCommandChanged(command);

				foreach (ContentElement key in Invokers.Keys)
				{
					//we can't hide ContentElements so we'll just disable it if relevant
					key.IsEnabled = (command.Status == CommandStatus.Enabled) && (command.Status == CommandStatus.Enabled);
				}
			}
		}
	}
}
